<?php
/**
 * 《所属节点》实现类
 *
 * @author 孙晓晔
 * @version $Id: Service.class.php 1051 2010-04-07 08:55:38Z sunxy $
 */

require_once(dirname(__FILE__) . '/WifiUtil.class.php');

class NodeEmp extends WifiUtil {
	public $tbl = Constant::tbl_wifi_node_emp;

	public $orderby = array(
		'-' => '请选择',
		'node' => '节点',
		'username' => '帐号',
			
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/03/06 11:35:48 $';
		$request['revision'] = '$Revision: 1.2 $';
			
		parent::__construct($request);
	}

	/**
	 * 预处理
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function pretreat($controller) {
		parent::pretreat($controller);
	}

	/**
	 * 首页
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function layout($controller, $request) {

		$controller->display($request, 'priv');
		return true;
	}

	/**
	 * 查找
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
			
		$controller->display($request, 'wifi/node/emp/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		
		$name_s = $request['name_s'];
		$node_s = $request['node_s'];

		$where = " WHERE 1=1";

		if (!empty($name_s)) {
			$where .= " AND (name like '%" . $name_s . "%' OR content like '%" . $name_s . "%')";
		}
		
		if (!empty($node_s)) {
			$where .= " AND node = '" . $node_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 * 列表
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'wifi/node/emp/list');
		return true;
	}

	function _service($code) {
		if (empty($code)) {
			return '';
		}

		return Common::name_code(Constant::tbl_service, $code);
	}

	/**
	 * 添加
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->assign_by_ref('emp_option', $this->id_option(Constant::tbl_emp, '1'));

		$controller->display($request, 'wifi/node/emp/add');
		return true;
	}

	/**
	 * 添加
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$service = $request['service'];
		$list = $request['list'];

		$bb = explode(',', $list);
			
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(service, emp, createtime) VALUES('%s', '%s', now())";

		while (list($k, $emp) = each($bb)) {
			if ($aa[1] == $emp) {
				continue;
			}

			$sql = sprintf($fmt, $this->tbl, $service, $emp);
			// debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'wifi/node/emp/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$node = $request['node'];
		
		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET emp = '%s', node = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $emp, $node, $content, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'wifi/node/emp/copy');
		return true;
	}
	
	function copyPost($controller, $request) {
		$this->pretreat($controller);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$node = $request['node'];
		
		$content = addslashes($request['content']);
		$status = $request['status'];
		
		$oper = $_SESSION['id'];
			
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, node, content, oper, status, createtime, modifytime) 
				VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $node, $content, $oper, $status);
		// debug($sql);
		$db->query($sql);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'wifi/node/emp/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['service_t'] = $this->_service($row['service']);

		$row = $this->_view_t($row);
		
		return $row;
	}

	/**
	 * 删除
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$insql = " in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		$sql = "DELETE FROM " . $this->tbl . " WHERE id " . $insql;
		$query = $db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _num($node) {			
		$db = newdb($this->dsn_r);
		$fmt = "SELECT COUNT(*) AS t FROM %s WHERE node = '%s'";
		$sql = sprintf($fmt, $this->tbl, $node);
		debug($sql);
		$result = $db->query($sql);
		$b = $db->fetch_array($result);
			
		$fmt = "SELECT COUNT(*) AS t FROM %s WHERE status = 1 AND node = '%s'";
		$sql = sprintf($fmt, $this->tbl, $node);
		debug($sql);
		$result = $db->query($sql);
		$a = $db->fetch_array($result);
			
		return $a['t'] . '/' . $b['t'];
	}
	
	function _audit($service, $emp) {
		if (empty($service)) {
			return false;
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE service = '%s' AND emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $service, $emp);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			if ($row['status'] == '1') {
				return true;
			}
		}

		return false;
	}
	
	function _reset($node) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET status = '2' WHERE node = '%s'";
		$sql = sprintf($fmt, $this->tbl, $node);
		debug($sql);
		$db->query($sql);
	}
	
	function _update($node, $emp) {
	
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
	
		// $service_x = Common::code(Constant::tbl_service, $code);
	
		// $node_x = $this->id($this->tbl, $node);
	
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE node = '%s' AND emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $node, $emp);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$fmt = "UPDATE %s SET status = '1', modifytime = now() WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $row['id']);
		} else {
			$fmt = "INSERT INTO %s(office, dept, emp, node, name, createtime, modifytime)
					VALUES('%s', '%s', '%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $node, $node);
		}
		debug($sql);
		$db->query($sql);
	}
	
	function _list_email($service) {
		if (empty($service)) {
			return false;
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE service = '%s'";
		$sql = sprintf($fmt, $this->tbl, $service);
		// echo $sql . "\n";
		$result = $db->query($sql);
		
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			if ($row['status'] != '1') {
				continue;
			}
			
			$emp = Common::emp($row['emp']);
			if (empty($emp['email']) || $emp['status'] == '3') {
				continue;
			}
			
			$ret[] = $emp['email'];
		}

		return $ret;
	}
	
	function _list($service) {
		if (empty($service)) {
			return false;
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE service = '%s'";
		$sql = sprintf($fmt, $this->tbl, $service);
		// echo $sql . "\n";
		$result = $db->query($sql);
		
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			if ($row['status'] != '1') {
				continue;
			}
			
			$row = $this->_view(null, $row);
			if ($row['emp_x']['status'] != '1') {
				continue;
			}
			
			$ret[] = $row['emp'];
		}

		return $ret;
	}
	
	function _node_emp($node, $emp) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE node = '%s' AND emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $node, $emp);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			if ($row['status'] == '1') {
				return true;
			}
		}
	
		return false;
	}

	function _copy($from, $to) {
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND status = '1'";
		$sql = sprintf($fmt, $this->tbl, $from);
		debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$this->_service_($to, $row);
		}
	}
	
	function _service_($to, $row) {
		$service = $row['service'];
		
		$emp = $to;
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
			
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND service = '%s'";
		$sql = sprintf($fmt, $this->tbl, $to, $service);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			return false;
		} else {
			$fmt = "INSERT INTO %s(office, dept, emp, service, createtime, modifytime) VALUES('%s', '%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $office, $dept, $to, $service);
			debug($sql);
			$db->query($sql);
		}	
		
		return true;
	}
	
	function view_emp($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$ret = $this->_list_emp($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'wifi/node/emp/_list');
		return true;
	}
	
	function t($controller, $request) {

		logdate($controller, $request);
		return true;
	}
}
?>
